package com.core;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.objects.Project;
import com.objects.Seat;
import com.objects.SeatMap;
import com.objects.SeatMapComparator;
import com.utilities.PLSConstants;

public class SearchEmployee {
	static Seat seat=null;
	public static boolean searchEmployeeSeatByID(int employeeID){
		boolean isValid =false;
		seat= searchEmployeeSeat(employeeID);
		if (seat.getEmployeeID().isEmpty()){
			isValid=false;
		}else{
			displayEmployeeSeatMap();
			isValid=true;
		}
		return isValid;
	}

	public static boolean exportEmployeaSeatMapToText() throws Exception{

		if (seat.getEmployeeID().isEmpty()){
			return false;
		}else{
			writeTextFile();
			return true;
		}
	}

	private static void writeTextFile() throws Exception{
		String seatMapFolder=PLSConstants.locationFolder;

		try{
			//path of file you want to write
			File file= new File(seatMapFolder + seat.getEmployeeID() + ".txt");
			FileWriter fWriter= new FileWriter(file);

			fWriter.write(getEmployeeSeatMapString());
			fWriter.close();
		}catch(Exception e){
			System.out.println(e);
			throw e;
		}

	}

	private static Seat searchEmployeeSeat(int employeeID){
		//call function which returns seat object passing only the employee ID
		//create current seatmap for testing
		SeatMap seatmap = new SeatMap("PIC","3F","A",1,2,"110001","222");
		SeatMap seatmap4 = new SeatMap("PIC","3F","A",2,2,"110001","222");
		SeatMap seatmap2 = new SeatMap("PIC","3F","A",2,1,"110001","222");
		SeatMap seatmap3 = new SeatMap("PIC","3F","A",3,1,"110001","222");
		List<SeatMap> seatmaps = new ArrayList<SeatMap>();
		seatmaps.add(seatmap);
		seatmaps.add(seatmap2);
		seatmaps.add(seatmap3);
		seatmaps.add(seatmap4);
		//create projects
		Project p1 = new Project("Chaos", "Helgeson Migration", "Developer");
		Project p2 = new Project("Chaos", "Helgeson Migration", "Developer");
		Project p3 = new Project("Chaos", "Helgeson Migration", "Developer");
		List<Project> projectList=new ArrayList<Project>();
		projectList.add(p1);
		projectList.add(p2);
		projectList.add(p3);

		//create the seat object
		Seat seat = new Seat("2","PTC12FA1-2","110001","Aaron","Macandili", null, projectList,"seattype","phonelocalnumber", seatmaps, "123" );
		return seat;
	}

	public static boolean displayEmployeeSeatMap(){
		boolean isValid =false;
		if(seat==null){
			isValid=false;
		}else{
			System.out.println(getEmployeeSeatMapString());
			isValid=true;
		}
		return isValid;
	}

	public static boolean displayEmployeeSeatMap(int employeeID){
		boolean isValid =false;
		if(seat==null){
			seat=searchEmployeeSeat(employeeID);
			if (seat.getEmployeeID().isEmpty()){
				isValid=false;
			}else{
				isValid=true;
				System.out.println(getEmployeeSeatMapString());
			}

		}else{
			isValid=true;
			System.out.println(getEmployeeSeatMapString());
		}
		return isValid;
	}

	private static String getEmployeeSeatMapString(){
		StringBuffer finalText = new StringBuffer();
		finalText.append("Name: " + seat.getLastName() + ", " + seat.getFirstName() + "\r\n");
		finalText.append(getProjectListString());

		finalText.append("Seat Code: " + seat.getSeatCode() +"\r\n" );
		finalText.append("Seat Type: " + seat.getSeatType()+"\r\n" );
		finalText.append("Quadrant Number: " + seat.getQuadrant()+"\r\n" );
		finalText.append("Local Number: " + seat.getPhoneLocalNumber()+"\r\n" );
		finalText.append("\r\n");
		finalText.append(getSeatMapString() + "\r\n");

		return finalText.toString();
	}

	private static String getProjectListString(){
		StringBuffer projects =new StringBuffer();
		projects.append("Project(s) :");

		List<Project> pl = new ArrayList<Project>();
		pl=seat.getProjects();

		//print all the projects for the current employee
		for(Project p: pl){
			projects.append(p.projectDescription + "\r\n");
		}

		return projects.toString();
	}

	private static String getSeatMapString(){
		List<SeatMap> sm = new ArrayList<SeatMap>();
		sm=seat.getSeatMap();
		Collections.sort(sm, new SeatMapComparator());

		StringBuffer sbSeatCode= new StringBuffer();
		StringBuffer sbEmpID= new StringBuffer();
		StringBuffer sbLocalNum= new StringBuffer();
		StringBuffer finalSeatMapText = new StringBuffer();

		int currentRow=0;

		SeatMap loopedSeatMap = null;
		for(int j=0; j==sm.size(); j++){
			loopedSeatMap=sm.get(j);
			if((currentRow == 0) | (currentRow==loopedSeatMap.getRow())){
				currentRow=loopedSeatMap.getRow();
				sbSeatCode.append(loopedSeatMap.getSeatLocationCode() + "\t" );
				sbLocalNum.append(loopedSeatMap.getLocalNumber() + "\t");

				//check if the current mapped seat is for the employee searched

				if (loopedSeatMap.getSeatLocationCode().equalsIgnoreCase(seat.getSeatCode()) && loopedSeatMap.getEmployeeId()==seat.getEmployeeID()){
					sbEmpID.append("**"+loopedSeatMap.getEmployeeId() +"**" + "\t");
				}else{
					sbEmpID.append(loopedSeatMap.getEmployeeId() + "\t");
				}
			}else{
				finalSeatMapText.append(sbSeatCode.toString() + "\r\n" + sbEmpID.toString() + "\r\n" + sbLocalNum.toString() +"\r\n" );

				sbSeatCode= new StringBuffer();
				sbEmpID= new StringBuffer();
				sbLocalNum= new StringBuffer();

				sbSeatCode.append(loopedSeatMap.getSeatLocationCode() + "\t" );
				sbLocalNum.append(loopedSeatMap.getLocalNumber() + "\t");

				if (loopedSeatMap.getSeatLocationCode().equalsIgnoreCase(seat.getEmployeeID()) && loopedSeatMap.getEmployeeId()==seat.getEmployeeID()){
					sbEmpID.append("**"+loopedSeatMap.getEmployeeId() +"**" + "\t");
				}else{
					sbEmpID.append(loopedSeatMap.getEmployeeId() + "\t");
				}
			}
			if (j==sm.size()){
				finalSeatMapText.append(sbSeatCode.toString() + "\r\n" + sbEmpID.toString() + "\r\n" + sbLocalNum.toString() +"\r\n" );
			}
		}
		return finalSeatMapText.toString();
	}
}
